 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Principals.Constantes;
import Model.Vehiculo;
import Presenter.UsuarioPresenter;
import Principals.Main;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;

/**
 *
 * @author Cristian
 */
public class VehiculoDAO {

    public static void registrarVehiculo(Vehiculo v) {
        try {
            Main.usrSesion.session.beginTransaction();
            Main.usrSesion.session.save(v);
            Main.usrSesion.session.getTransaction().commit();
        } catch (Exception e) {
            System.out.println("MENSAJE DE ERROR:" + e);
        } finally {
            UsuarioPresenter.registrarLogUsuario(Main.usr, "vehiculo", v.getIdVehiculo() + "->" + v.getPlaca(), Constantes.LOG_INSERT);
        }
    }

    public static List<Vehiculo> buscarVehiculo(String placa, int anho, int estado) {
        try {
            String q = "from Vehiculo where estado!=0 ";

            q += " AND anho <= :anho ";

            if (placa != null) {
                q += " AND lower(placa) LIKE lower(:placa)";
            }

            if (estado != 0) {
                q += " AND estado = :estado ";
            }
            
            Main.usrSesion.session.beginTransaction();
            Query query = Main.usrSesion.session.createQuery(q);
            if (placa != null) {
                query.setParameter("placa", "%" + placa + "%");
            }
            if (estado != 0) {
                query.setParameter("estado", estado);
            }
            query.setParameter("anho", anho);
            Main.usrSesion.session.getTransaction().commit();
            return query.list();
        } catch (Exception e) {
            System.out.println("MENSAJE DE ERROR:" + e);
            return null;
        }

    }

    public static Vehiculo cargarVehiculo(int idVehiculo) {
        String q = "from Vehiculo where id=:id";

        Main.usrSesion.session.beginTransaction();
        Query query = Main.usrSesion.session.createQuery(q);
        query.setParameter("id", idVehiculo);
        Vehiculo v = (Vehiculo) query.uniqueResult();
        Main.usrSesion.session.getTransaction().commit();
        return v;
    }

    public static Vehiculo modificarVehiculo(Vehiculo v) {
        try {
            Main.usrSesion.session.beginTransaction();
            Main.usrSesion.session.update(v);
            Main.usrSesion.session.getTransaction().commit();
        } catch (Exception e) {
            System.out.println("MENSAJE DE ERROR:" + e);
        } finally {
            UsuarioPresenter.registrarLogUsuario(Main.usr, "vehiculo", v.getIdVehiculo() + "->" + v.getPlaca(), Constantes.LOG_INSERT);
        }
        return v;
    }

    public static List<Vehiculo> vehiculosDisponibles() {
        Main.usrSesion.session.beginTransaction();
        Query query = Main.usrSesion.session.createQuery("from Vehiculo where estado = 1");
        Main.usrSesion.session.getTransaction().commit();
        return query.list();
    }

    public static int verificarPlaca(String placaVehiculo) {
        Main.usrSesion.session.beginTransaction();
        Query query = Main.usrSesion.session.createQuery("from Vehiculo where placa=:placa");
        query.setParameter("placa", placaVehiculo);
        Main.usrSesion.session.getTransaction().commit();
        return query.list().size();
    }

    public static int verificarPlacaModificar(int idVehiculo, String placaVehiculo) {
                Main.usrSesion.session.beginTransaction();
        Query query = Main.usrSesion.session.createQuery("from Vehiculo where placa=:placa and not id_vehiculo=:idvehiculo");
        query.setParameter("idvehiculo", idVehiculo);
        query.setParameter("placa", placaVehiculo);        
        Main.usrSesion.session.getTransaction().commit();
        return query.list().size();
    }
    
    public static List<Vehiculo> buscarVehiculosSimulacion(String lista){
        Main.usrSesion.session.beginTransaction();
        Query query = Main.usrSesion.session.createQuery("from Vehiculo where id_vehiculo not in " + lista);
        Main.usrSesion.session.getTransaction().commit();
        return query.list();
    }
    
}
